/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webapp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import webapp.model.RestaurantManager;
import webapp.model.Restaurant;
import webapp.model.Cursor;

/**
 *
 * @author Xiao
 */
public class searchServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
		try {
			String url = "/search.jsp"; // default
			request.setAttribute("title", "Search Results");

			String type = request.getParameter("Type");
			String price = request.getParameter("Price");
			String rating = request.getParameter("Rating");
			String keywords = request.getParameter("keyword");
			request.setAttribute("Type", type);
			request.setAttribute("Price", price);
			request.setAttribute("Rating", rating);
			request.setAttribute("keyword", keywords);
			

			RestaurantManager rm = new RestaurantManager();
			Cursor cur = new Cursor();
			Restaurant[] search_results = null;
			cur = new Cursor(rm.search(type, price, rating, keywords));
			cur.setPageSize(20);
			search_results = cur.nextPage();

			String show_page = request.getParameter("show_page");
			int show_page_num = 0;
			if (show_page != null && !show_page.equals("")) {
				show_page_num = Integer.parseInt(show_page);
				if (show_page_num > 0 && show_page_num <= cur.getNumOfPages()) {
					for (int i = 0; i < show_page_num; i++) {
						search_results = cur.nextPage();
					}
				}
			}
			boolean has_next = cur.hasNextPage();
			boolean has_prev = cur.hasPreviousPage();

			if (search_results != null) {
				request.setAttribute("search_results", search_results);
				request.setAttribute("show_page", show_page_num);
				request.setAttribute("next_page", has_next);
				request.setAttribute("prev_page", has_prev);
			} else {
				url = "/feedback.jsp";
				request.setAttribute("feedback_message", "No results found.");
			}

			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher(url);
			dispatcher.forward(request, response);
		} finally {
			out.close();
		}
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
